package edu.zjut.hjg.saas.web.controller.tenant;

import static edu.zjut.hjg.saas.web.support.SaaSConstants.SESSION_NAME_TENANT;

import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.zjut.hjg.saas.biz.model.Tenant;
import edu.zjut.hjg.saas.biz.service.TenantService;
import edu.zjut.hjg.saas.biz.service.exception.ServiceException;
import edu.zjut.hjg.saas.common.annotation.CookieSessionIn;
import edu.zjut.hjg.saas.common.util.ParameterUtil;
import edu.zjut.hjg.saas.web.model.TenantSession;
import edu.zjut.hjg.saas.web.model.form.UpdateTenantForm;

/**
 * 查看、修改租户信息
 *
 * @author hjg
 */
@Controller
@RequestMapping("/tenant/profile")
public class ProfileController {

    private static final Log log = LogFactory.getLog(ProfileController.class);

    @Autowired
    private TenantService    tenantService;

    @RequestMapping("/info.htm")
    @CookieSessionIn(value = SESSION_NAME_TENANT, param = "session")
    public void info(TenantSession session, Model model) {
        Tenant tenant = tenantService.findById(session.getId());
        UpdateTenantForm tenantForm = new UpdateTenantForm();
        tenantForm.setContact(tenant.getContact());
        tenantForm.setEmail(tenant.getEmail());
        tenantForm.setPhone(tenant.getPhone());
        model.addAttribute("tenant", tenant);
        model.addAttribute("tenantForm", tenantForm);
    }

    @RequestMapping(value = "/updateInfo.htm", method = RequestMethod.POST)
    @CookieSessionIn(value = SESSION_NAME_TENANT, param = "session")
    public String updateInfo(@Valid @ModelAttribute("tenantForm") UpdateTenantForm tenantForm,
                             BindingResult result, TenantSession session) {
        if (result.hasErrors()) {
            return "redirect:/tenant/profile/info.htm";
        }
        Tenant tenant = tenantService.findById(session.getId());
        tenant.setContact(tenantForm.getContact());
        tenant.setEmail(tenantForm.getEmail());
        tenant.setPhone(tenantForm.getPhone());
        tenantService.update(tenant);
        return "redirect:/tenant/profile/info.htm";
    }

    @RequestMapping(value = "/updatePassword.htm", method = RequestMethod.POST)
    @CookieSessionIn(value = SESSION_NAME_TENANT, param = "session")
    public String updatePassword(String oldPassword, String newPassword, TenantSession session,
                                 Model model) {
        if (!ParameterUtil.notBlank(oldPassword, newPassword)) {
            return "tenant/profile/password";
        }
        try {
            tenantService.updatePassword(session.getId(), oldPassword, newPassword);
        } catch (ServiceException e) {
            log.warn(e.getMessage() + "," + e.getErrorMessage(), e);
            model.addAttribute("errorMsg", e.getErrorMessage());
            return "tenant/profile/password";
        }
        model.addAttribute("msgCode", "message.updatePassword.success");
        return "tenant/profile/password";
    }
}
